package kr.hkit.score_pjt.ListUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JTable;

import kr.hkit.framework.dao.LoadPropertyDAO;
import kr.hkit.score_pjt.DAO.StdInfoDao;
import kr.hkit.score_pjt.DTO.StdInfo;

public class StdInfoListUi extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private Connection con;
	private StdInfoDao dao;
	private ArrayList<StdInfo> stdLists;
	private MyTableModel model;
	
	/**
	 * Create the frame.
	 */
	public StdInfoListUi(ArrayList<StdInfo> stdLists) {
		this.stdLists = stdLists;
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("성 적 현 황");
		lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 18));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblNewLabel, BorderLayout.NORTH);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		model = new MyTableModel();
		table = new JTable(model);	
		scrollPane.setViewportView(table);
		alignData();
	}
	
	public void refreshList(ArrayList<StdInfo> stdLists){
		this.stdLists = stdLists;
		model.fireTableDataChanged();
		alignData();
	}
	
	public void alignData(){
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		for(int i=0; i<4; i++){
			table.getColumnModel().getColumn(i).setCellRenderer(dtcrCenter);
		}
		
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		for(int i=4; i<9; i++){
			table.getColumnModel().getColumn(i).setCellRenderer(dtcrRight);
		}
	}
	
	class MyTableModel extends AbstractTableModel{
		final String[] COLUMN_NAME = {"등급", "순위", "학번", "성명", "국어", "영어", "수학", "총점", "평균"};
				
		public int getRowCount() {
			return stdLists.size();
		}

		public int getColumnCount() {
			return COLUMN_NAME.length;
		}
	
		public String getColumnName(int column) {
			return COLUMN_NAME[column];
		}

		public Object getValueAt(int row, int col) {
			StdInfo std = stdLists.get(row);
			switch(col){
			case 0:
				return std.getStdGrade();
			case 1:
				return std.getStdRank();
			case 2:
				return std.getStdNum();
			case 3:
				return std.getStdName();
			case 4:
				return std.getKorScore();
			case 5:
				return std.getEngScore();
			case 6:
				return std.getMathScore();
			case 7:
				return std.getTotal();
			case 8: 
				return std.getAvg();
			}
			
			return null;
		}
		
	}

}
